#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <netinet/in.h>
#include<signal.h>
int sockfd;
void my_exit(int sig)
{
    shutdown(sockfd,SHUT_RDWR);
    exit(1);
}
int main(int argc,char **argv)
{
    
    struct sockaddr_in src_addr;
    struct sockaddr_in dest_addr;
    socklen_t len;
    char msg[1024]={0};
    signal(SIGINT,my_exit);
    if((sockfd = socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        perror("socket error\n");
        exit(1);

    }
    src_addr.sin_family=AF_INET;
    src_addr.sin_port=htons(6666);
    src_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    bind(sockfd,(struct sockaddr*)&src_addr,sizeof(struct sockaddr));
    // dest_addr.sin_family=AF_INET;
    // dest_addr.sin_port=htons(5555);
    // dest_addr.sin_addr.s_addr=inet_addr("10.2.8.3");
    while (1)
    {
        
        len = sizeof(struct sockaddr_in);
        recvfrom(sockfd,msg,sizeof(msg),MSG_NOSIGNAL,(struct sockaddr*)&dest_addr,&len);
        if(strlen(msg)>0)
        { 
        printf("%s\n",msg);
        memset(msg,0,1024);
        }
    }
    
    return 0;
}